package controllers;

import play.*;
import play.mvc.*;
import play.data.*;

import models.*;
import views.html.member.*;
import views.html.application.index;
import static play.data.Form.*;
import java.util.List;
import play.i18n.*;

public class Member extends Controller {

	public static Result join(String lang) {
		Logger.info(lang);
		return ok(register.render(form(RegisterForm.class), Lang.forCode(lang)));
	}

	// POST
	public static Result register() {
		Form<RegisterForm> registerForm = form(RegisterForm.class).bindFromRequest();

		if(registerForm.hasErrors()) {
			return badRequest(register.render(registerForm, Lang.forCode(registerForm.field("lang").value())));
		} else {
			String username = registerForm.get().username;
			String password = registerForm.get().password;
			String email = registerForm.get().email;
			String credit_card = registerForm.get().credit_card;

			String hashedPW = MD5Hash.digest(password);

			if (hashedPW == null) {
				//TODO: Error message
				return redirect(routes.Application.index(registerForm.field("lang").value()));
			}

			User user = new User(username, hashedPW, email, credit_card, 0);
			user.save();

			String uuid = LogSession.createSession(username, 0);
            session("uuid", uuid);

			flash("success", "You have successfully registered an account.");
			return redirect(routes.Application.index(registerForm.field("lang").value()));
		}

	}

	public static Result requestRefund(long id, String lang) {

		String uuid = session("uuid");
		String username = LogSession.getUsernameFromUUID(uuid);

		if(username == null)
			return internalServerError("User not found");

		if(!Payment.isPaymentOwner(username, id))
			return internalServerError("You are not the Payment Owner");

		Payment payment = Payment.findByID(id);

		if(payment == null || payment.status != 0)
			return internalServerError("Payment Error");

		payment.status = 1;
		payment.save();

		return redirect(routes.Member.transaction(lang));
	}

	public static Result cancelRefund(long id, String lang) {

		String uuid = session("uuid");
		String username = LogSession.getUsernameFromUUID(uuid);

		if(username == null)
			return internalServerError("User not found");

		if(!Payment.isPaymentOwner(username, id))
			return internalServerError("You are not the Payment Owner");

		Payment payment = Payment.findByID(id);

		if(payment == null || payment.status != 1)
			return internalServerError("Payment Error");

		payment.status = 0;
		payment.save();

		return redirect(routes.Member.transaction(lang));
	}



	@With(LogSession.class)
	@Security.Authenticated(Secured.class)
    public static Result profile(String lang) {
		String uuid = session("uuid");
		String username = LogSession.getUsernameFromUUID(uuid);

		User user = User.findByUser(username);

		if(user != null)
			return ok(profile.render(user,Lang.forCode(lang)));
		else
			return internalServerError("User not found");
    }

	@With(LogSession.class)
	@Security.Authenticated(Secured.class)
    public static Result transaction(String lang) {
		String uuid = session("uuid");
		String username = LogSession.getUsernameFromUUID(uuid);

		List<Payment> payment = Payment.findUserAllActivePayment(username, lang);
    	return ok(transaction.render(payment, Lang.forCode(lang)));
    }

	@With(LogSession.class)
	@Security.Authenticated(Secured.class)
    public static Result refund(String lang) {
    	String uuid = session("uuid");
		String username = LogSession.getUsernameFromUUID(uuid);

		List<Payment> processing = Payment.findByUsernameAndStatus(username, lang, 1);
		List<Payment> approved = Payment.findByUsernameAndStatus(username, lang, 2);
		List<Payment> rejected = Payment.findByUsernameAndStatus(username, lang, 3);

    	return ok(refund.render(processing, approved, rejected, Lang.forCode(lang)));
    }

}